package fregat.base.ui;

import javax.swing.*;
import java.awt.*;
import java.util.Vector;

/**
 * Model data for the {@link at.ecolog.base.gui.AttributeRenderer}.
 *
 * @author Alexander Mann <br><b><i>ECO_L O G</i> &nbsp Logistiksysteme GmbH</b>
 * @version 1.0
 */
public class ListElement {

  public static final int INT = 0;
  public static final int TEXT = 1;
  public static final int BOOL = 2;
  public static final int COMMENT = 3;
  public static final int LIST = 4;

  private Vector attributes = new Vector();
  private String strName;
  private String strCommit;

  public ListElement(String pstrName) {
    this(pstrName, "");
  }

  public ListElement(String pstrName, String pstrCommit) {
    strName = pstrName;
    strCommit = pstrCommit;
  }

  public void addAttribute(String strAttributeName, int intType, int intWidth) {
    JComponent component = null;
    switch (intType) {
      case INT:
      case TEXT:
        component = new JTextField();
        int intCharacter = component.getFontMetrics(component.getFont()).stringWidth("0");
        component.setPreferredSize(new Dimension(intWidth * intCharacter + 6, 20));
        break;
      case BOOL:
        component = new JCheckBox();
        break;
      case COMMENT:
        component = new JTextArea();
        component.setFont(new JLabel().getFont());
        component.setOpaque(false);
        break;
      case LIST:
        component = new JComboBox();
        break;
    }

    if (component != null) {
      component.setName(strAttributeName);
      attributes.addElement(new Attribute(intType, component));
    }
  }

  public Object getAttributeValue(String strAttributeName) {
    Object value = null;
    int i = getAttributeIndex(strAttributeName);
    Attribute attribute = (Attribute) attributes.elementAt(i);

    switch (attribute.intType) {
      case INT:
        try {
          value = new Integer(((JTextField) attribute.component).getText());
        } catch (NumberFormatException ex) {
          value = new Integer(0);
        }
        break;
      case TEXT:
        value = ((JTextField) attribute.component).getText();
        break;
      case COMMENT:
        value = ((JTextArea) attribute.component).getText();
        break;
      case BOOL:
        value = new Boolean(((JCheckBox) attribute.component).isSelected());
        break;
      case LIST:
        value = ((JComboBox) attribute.component).getSelectedItem();
        break;
    }

    return value;
  }

  public void setAttributeValue(String strAttributeName, Object value) {
    int i = getAttributeIndex(strAttributeName);
    Attribute attribute = (Attribute) attributes.elementAt(i);

    switch (attribute.intType) {
      case INT:
      case TEXT:
        ((JTextField) attribute.component).setText(value.toString());
        break;
      case COMMENT:
        ((JTextArea) attribute.component).setText(value.toString());
        break;
      case BOOL:
        ((JCheckBox) attribute.component).setSelected(((Boolean) value).booleanValue());
        break;
      case LIST:
        ((JComboBox) attribute.component).setSelectedItem(value);
        break;
    }
  }

  public void setAttributeModel(String strAttributeName, Object model) {
    int i = getAttributeIndex(strAttributeName);
    Attribute attribute = (Attribute) attributes.elementAt(i);
    if (attribute.intType == LIST) {
      ((JComboBox) attribute.component).setModel((ComboBoxModel) model);
    }
  }

  private int getAttributeIndex(String strAttributeName) {
    int i = 0;
    while (!((Attribute) attributes.elementAt(i)).component.getName().equalsIgnoreCase(strAttributeName)) i++;
    return i;
  }

  public JComponent[] getAttributes() {
    JComponent[] components = new JComponent[attributes.size()];
    for (int i = 0; i < attributes.size(); i++) {
      components[i] = (JComponent) ((Attribute) attributes.elementAt(i)).component;
    }
    return components;
  }

  public String toString() {
    return strName;
  }

  public String getName() {
    return strName;
  }

  public String getCommit() {
    return strCommit;
  }

  class Attribute {
    public int intType;
    public JComponent component;

    public Attribute(int pintType, JComponent pcomponent) {
      intType = pintType;
      component = pcomponent;
    }
  }
}
